
// 路由配置
import $router from '@/router'
import $ajax from '../$ajax';
import $app from '../$app';

export default {

    // 专利申请 $refs['baseTop'] 基本信息 判断专用
    pBaseTop_judge(that, name, attorney_path) {
        if (!$app.judgeData($app.delSpace(name))) {
            that.$Message.error({
                content: "专利名称不能为空"
            });
            that.$refs["baseTop"].$refs["zName1"].focus();
            that.$refs["baseTop"].zName1 = "";
            return false;
        }

        if (!$app.judgeData(attorney_path)) {
            $app.alert("代理委托书不能为空");
            document.getElementById("cat-body").scrollTo(0, 0);
            return false;
        }

        return true;
    },


    // 专利申请 $refs['common'] 申请人判断专用
    pCom_judge(that, applicant_list, inventor_list) { // 身份证正则 - 香港人身份证，通过不了

        // 申请人 for循环 判断
        for (let i = 0; i < applicant_list.length; i++) {

            if (!$app.judgeData($app.delSpace(applicant_list[i].name))) {
                that.$Message.error({
                    content: '申请人' + (i + 1) + '：名称不能为空'
                });
                that.$nextTick(() => {
                    that.$refs['common'].$refs['T_name' + i][0].focus();
                    that.$refs['common'].applicant_list[i].name = '';
                });
                return false;
            }

            if (!$app.judgeData($app.delSpace(applicant_list[i].type))) {
                that.$Message.error({
                    content: '申请人' + (i + 1) + '：类型不能为空'
                });
                return false;
            }

            if (!$app.judgeData($app.delSpace(applicant_list[i].id_card))) {
                that.$Message.error({
                    content: '申请人' + (i + 1) + '：证件代码不能为空'
                });
                that.$nextTick(() => {
                    that.$refs['common'].$refs['T_id_card' + i][0].focus();
                    that.$refs['common'].applicant_list[i].id_card = '';
                });
                return false;
            }
            if (!$app.judgeData($app.delSpace(applicant_list[i].zip_code))) {
                that.$Message.error({
                    content: '申请人' + (i + 1) + '：邮政编码不能为空'
                });
                that.$nextTick(() => {
                    that.$refs['common'].$refs['T_zip_code' + i][0].focus();
                    that.$refs['common'].applicant_list[i].zip_code = '';
                });
                return false;
            }
            if (!$app.judgeData($app.delSpace(applicant_list[i].address_detail))) {
                that.$Message.error({
                    content: '申请人' + (i + 1) + '：联系地址不能为空'
                });
                that.$nextTick(() => {
                    that.$refs['common'].$refs['T_address' + i][0].focus();
                    that.$refs['common'].applicant_list[i].address_detail = '';
                });
                return false;
            }

            if (applicant_list[i].nationality == 'CN') {
                if (!$app.judgeData($app.delSpace(applicant_list[i].province_code))) {
                    that.$Message.error({
                        content: '申请人' + (i + 1) + '：地区第一级不能为空'
                    });
                    return false;
                }

                if (!$app.judgeData($app.delSpace(applicant_list[i].city_code))) {
                    that.$Message.error({
                        content: '申请人' + (i + 1) + '：地区第二级不能为空'
                    });
                    return false;
                }
            }

        }


        // 发明人 for循环 判断
        for (let i = 0; i < inventor_list.length; i++) {


            if (!$app.judgeData($app.delSpace(inventor_list[i].name))) {
                that.$Message.error({
                    content: '发明人'+(i+1)+'：姓名不能为空'
                });
                that.$refs['common'].$refs['i_name'+i][0].focus();
                that.$refs['common'].inventor_list[i].name = '';
                return false;
            }
            
            // 身份证 只判断发明人1
            if(i<1){
                if (!$app.judgeData($app.delSpace(inventor_list[i].id_card))) {
                    that.$Message.error({
                        content: '发明人'+(i+1)+'：身份证号不能为空'
                    });
                    that.$refs['common'].$refs['i_id_card'+i][0].focus();
                    that.$refs['common'].inventor_list[i].id_card = '';
                    return false;
                }
            }
            

        }


        

        


        return true;
    },

    //专利提交-其他-优先权声明-判断
    p_d_l_judge(that) {
        let {
            priority_declar_list
        } = that;
        if ($app.judgeData(priority_declar_list)) {
            for (let i = 0; i < priority_declar_list.length; i++) {


                if (!$app.judgeData($app.delSpace(priority_declar_list[i].priority_declar_name))) {
                    that.$Message.error({
                        content: '优先权声明' + (i + 1) + '：原代理机构注册国籍不能为空'
                    });

                    // 聚焦
                    that.$nextTick(() => {
                        that.$refs['priority_declar_name' + i][0].isFocused = true;
                        that.$refs['priority_declar_name' + i][0].visible = true;
                        // $app.scroll_to_top(that.$refs['priority_declar_name' + i][0].$el.offsetTop);
                    });

                    return false;
                }

                if (priority_declar_list[i].priority_declar_date) {

                    //DatePicker 先天性的bug =》再处理
                    if (typeof priority_declar_list[i].priority_declar_date === 'string') {
                        //ok 的日期
                    } else {
                        // 转成日期
                        that.priority_declar_list[i].priority_declar_date = ($app.formatTime(2, ((priority_declar_list[i].priority_declar_date).valueOf() / 1000)))
                    }

                } else {
                    that.$Message.error({
                        content: '优先权声明' + (i + 1) + '：在先申请日不能为空'
                    });

                    // 聚焦
                    that.$nextTick(() => {
                        that.$refs['priority_declar_date' + i][0].isFocused = true;
                        that.$refs['priority_declar_date' + i][0].visible = true;
                        // $app.scroll_to_top(that.$refs['priority_declar_date' + i][0].$el.offsetTop);
                    });

                    return false;
                }

                if (!$app.judgeData($app.delSpace(priority_declar_list[i].priority_declar_number))) {
                    that.$Message.error({
                        content: '优先权声明' + (i + 1) + '：在先申请号不能为空'
                    });
                    that.$nextTick(() => {
                        that.$refs['priority_declar_number' + i][0].focus();
                        that.priority_declar_list[i].priority_declar_number = '';
                    });

                    return false;
                }
            };
        }


        return true;
    },

    // 分案申请 判断
    fenAn_jud(that) {
        let {
            div_app_number, div_app_date
        } = that;
        if ((div_app_number) || (div_app_date)) {
            if (!$app.delSpace(div_app_number)) {
                that.$Message.error({ content: '分案申请，请输入原申请号。' });
                that.$nextTick(() => {
                    that.$refs['div_app_number'].focus();
                    that.div_app_number = '';
                });
                return false;
            }

            if (div_app_date) {

                //DatePicker 先天性的bug =》再处理
                if (typeof div_app_date === 'string') {
                    //ok 的日期
                } else {
                    // 转成日期
                    that.div_app_date = ($app.formatTime(2, ((div_app_date).valueOf() / 1000)))
                }

            } else {
                that.$Message.error({ content: '分案申请，请选择原申请日期。' });


                // 聚焦
                that.$nextTick(() => {
                    that.$refs['div_app_date'].isFocused = true;
                    that.$refs['div_app_date'].visible = true;
                    // $app.scroll_to_top(that.$refs['div_app_date'].$el.offsetTop);
                });

                return false;
            }

        }
        return true;
    },


    

    //恭喜发财，可以提交，配合上者使用，备注太啰嗦，不写了
    gweQRT(that, obj) {
        //申请人列表json串=变成数组
        let applicant_list_A = JSON.parse(obj.applicant_list);
        let applicant_list_B = [];
        applicant_list_A.forEach((val, i) => {
            let { cityList, ...dataJ } = val;//删除对象里面的cityList=>总之够恶心了
            applicant_list_B.push(dataJ)
        });


        // 再次赋值
        obj.applicant_list = JSON.stringify(applicant_list_B);

        //专利提交
        $ajax.patentSubmit(obj, (resData) => {
            if ($app.judgeData(obj.order_id)) {//有值 修改订单
                $app.toast('订单修改成功', true);
                $app.vontW();
            } else {//没值 首次提交
                $app.payBoxShow(that, resData.order_id);//调起支付框
            }
        });
    },




    //返回订单详情
    backOrSee() {
        $app.vontW();//普通商品编辑
    },

    //修改页面=》返回详情页
    vontW() {
        let {
            name,
            query: {
                order_id,
                type
            }
        } = $router.history.current;
        // 撰写项目
        if ($app.is103_105_132(type)) {
            $app.closeTag($router.history.current); // 关闭本页面

            //页面跳转
            $router.push({
                name: 'patentThird',
                query: { order_id }
            });
            
        } else {
            let nameDo = "";

            $app.payInline.forEach((val, i) => {
                type == val.type && (nameDo = val.infoPage);
            });
            $app.closeTag($router.history.current); // 关闭本页面

            //页面跳转
            $router.push({
                name: nameDo,
                query: { order_id }
            });
        }


    },

    //版权修改
    wetRK(that) {
        let {
            $route: {
                name
            },
            orderInfo: {
                people_info: {
                    people_type,
                    people_country,
                    people_province,
                    people_city,
                    people_name,
                    cert_type,
                    cert_number,
                    reg_people_name,
                    reg_people_contact,
                    reg_people_phone,
                    reg_people_tel,
                    reg_people_email,
                    reg_people_address
                }
            }
        } = that;


        //C1 基础信息取值
        that.$refs['C1'].people_type = people_type;
        that.$refs['C1'].people_country = people_country;


        that.$refs['C1'].people_name = people_name;
        that.$refs['C1'].cert_type = cert_type;
        that.$refs['C1'].cert_number = cert_number;


        that.$refs['C1'].addObj = {
            people_province,
            people_city
        };




        //C2
        that.$refs['C2'].reg_people_name = reg_people_name;
        that.$refs['C2'].reg_people_contact = reg_people_contact;
        that.$refs['C2'].reg_people_phone = reg_people_phone;
        that.$refs['C2'].reg_people_tel = reg_people_tel;
        that.$refs['C2'].reg_people_email = reg_people_email;
        that.$refs['C2'].reg_people_address = reg_people_address;

        //版权：美术DoMS、软件DoRJ 通过这个来判断
        if (name.indexOf('DoMS') > -1) {//美术
            let {
                orderInfo: {
                    works_name,
                    author_name,
                    finish_time,
                    city,
                    publish_state,
                    publish_time,
                    publish_city,
                    right_gs_desc,
                    desc_word_file_path,
                    works_file_path
                }
            } = that;
            that.works_name = works_name;
            that.author_name = author_name;
            that.finish_time = finish_time;
            that.city = city;
            that.publish_state = publish_state;
            that.publish_time = publish_time;
            that.publish_city = publish_city;

            if (right_gs_desc == '个人作品') {
                that.right_gs_desc = 1;
            } else if (right_gs_desc == '合作作品') {
                that.right_gs_desc = 2;
            } else if (right_gs_desc == '法人作品') {
                that.right_gs_desc = 3;
            }

            //说明文档
            that.desc_word_file_path = desc_word_file_path;
            that.wordText = $app.urlToName(desc_word_file_path);

            //作品图样
            that.works_file_path = works_file_path;

        } else {//软件著作

            let {
                orderInfo: {
                    software_name,
                    software_short_name,
                    version_no,
                    finish_time,
                    publish_state,
                    publish_time,
                    publish_city,
                    program_lang,
                    code_row_num,
                    hardware_env,
                    software_env,
                    main_func,
                    code_word_file_path,
                    desc_word_file_path
                }
            } = that;
            that.software_name = software_name;
            that.software_short_name = software_short_name;
            that.version_no = version_no;
            that.finish_time = finish_time;
            that.publish_state = publish_state;
            that.publish_time = publish_time;
            that.publish_city = publish_city;
            that.program_lang = program_lang;
            that.code_row_num = code_row_num;
            that.hardware_env = hardware_env;
            that.software_env = software_env;
            that.main_func = main_func;

            //上传源程序
            that.code_word_file_path = code_word_file_path;
            that.codeText = $app.urlToName(code_word_file_path);

            //上传说明文档
            that.desc_word_file_path = desc_word_file_path;
            that.descText = $app.urlToName(desc_word_file_path);
        }




    },
    //商标提交正则判断
    tmSubmitZ(that, obj, order_id, bz_id) {
        console.log('最终提交，商标 = ', obj);

        let {
            $route: {
                query: {
                    type//type 200普通注册，209担保注册
                }
            }
        } = that;

        let {
            img_path,//商标图样不能为空
            is_portrait,//是否以肖像作为商标申请注册,false否,true是
            portrait_path,
            reg_people_name,//公司名称
            
            business_license_number,//企业证件号
            idcard,//身份证

            business_license_address, //长地址
            business_license_province,//省份名称 中文汉字
            business_license_province_code,//省份代码
            business_license_city,//城市名称 中文汉字
            business_license_city_code,//城市代码
            business_license_detail_address,//地址(短地址)
            
            attorney_path,//委托书
            idcard_path,//身份证复印件 地址 pdf


            business_license_path,//营业执照 pdf

            reg_people_type//1企业 2个体工商户
        } = obj;

        if (!$app.judgeData(img_path)) {
            $app.alert('商标图样不能为空', '温馨提示', () => {
                if (obj.type == 1) {//1文字，2图形，3文字及图形
                    that.$refs["B1"].$refs['zName1'].focus();
                }
            });
            document.getElementById('cat-body').scrollTo(0, 0);
            return;
        }


        // if ($app.judgeData(is_portrait) && !$app.judgeData(portrait_path)) {
        //     $app.alert('肖像证明文件不能为空');
        //     document.getElementById('cat-body').scrollTo(0, 0);
        //     return;
        // }
        
        ////////////////////////////// 1企业 2个体工商户 - 都要判断 都有营业执照+地址 ////////////////////////////// 
        if (!$app.judgeData(business_license_path)) {
            $app.alert('请上传营业执照');
            return;
        }
        if (!$app.judgeData($app.delSpace(business_license_number))) {
            that.$Message.error({ content: '营业执照不能为空' });
            that.$refs["B2"].$refs['business_license_number'].focus();
            that.$refs["B2"].business_license_number = '';
            return;
        }

        if (!business_license_province) {
            that.$Message.error({ content: '请选择省份' });
            return;
        }
        
        if (!business_license_city) {
            /**
             * 省份代码：以下的没有市
             * 710000 台湾
             * 810000 香港
             * 820000 澳门
            */
            if(business_license_province_code=='710000'||business_license_province_code=='810000'||business_license_province_code=='820000'){
                //无需处理
            }else{
                that.$Message.error({ content: '请选择市区' });
                return;
            }
        }

        if (!$app.judgeData($app.delSpace(business_license_detail_address))) {
            that.$Message.error({ content: '地址不能为空' });
            that.$refs["B2"].$refs['business_license_detail_address'].focus();
            that.$refs["B2"].business_license_detail_address = '';
            return;
        }
        ////////////////////////////// 1企业 2个体工商户 - 都要判断 都有营业执照+地址 ////////////////////////////// 

        

        //1企业 2个体工商户
        if (reg_people_type == 1) {
            if (!$app.judgeData($app.delSpace(reg_people_name))) {
                that.$Message.error({ content: '公司名称不能为空' });
                that.$refs["B2"].$refs['reg_people_name'].focus();
                that.$refs["B2"].reg_people_name = '';
                return;
            }
            
            
        } else {

            if (!$app.judgeData(idcard_path)) {
                $app.alert('请上传身份证复印件');
                return;
            }

            if (!$app.judgeData($app.delSpace(reg_people_name))) {
                that.$Message.error({ content: '申请人名称不能为空' });
                that.$refs["B2"].$refs['reg_people_name'].focus();
                that.$refs["B2"].reg_people_name = '';
                return;
            }

            if (!$app.judgeData($app.delSpace(idcard))) {
                that.$Message.error({ content: '个体工商户：证件号码不能为空' });
                that.$refs["B2"].$refs['idcard'].focus();
                that.$refs["B2"].idcard = '';
                return;
            }


        }

        if (!$app.judgeData(attorney_path)) {
            $app.alert('请上传委托书');
            return;
        }

        $ajax.tmSubmit(obj, resData => {

            if ($app.judgeData(order_id) || $app.judgeData(bz_id)) {//有值 修改订单
                $app.toast(($app.judgeData(bz_id) ? '提交成功' : '订单修改成功'), true);
                $app.vontW();
            } else {//没值 首次提交
                $app.payBoxShow(that, resData.order_id);//调起支付框
            }

        });
    },

    //基础信息+联系方式，正则判断，其中有一个为false，就会断开不往下走
    banQbase(that, obj, callBack) {
        if (!$app.judgeData(obj.people_province) || !$app.judgeData(obj.people_city)) {
            that.$Message.error({ content: '请选择著作权人省市' });
            document.getElementById('cat-body').scrollTo(0, 0);
            return
        }

        if (!$app.judgeData($app.delSpace(obj.people_name))) {
            that.$Message.error({ content: '自然人/机构名称，不能为空' });
            that.$refs['C1'].$refs['people_name'].focus();
            that.$refs['C1'].people_name = '';
            return
        }

        if (!$app.judgeData($app.delSpace(obj.cert_number))) {
            that.$Message.error({ content: '证件号码，不能为空' });
            that.$refs['C1'].$refs['cert_number'].focus();
            that.$refs['C1'].cert_number = '';
            return
        }

        if (!$app.judgeData($app.delSpace(obj.reg_people_name))) {
            that.$Message.error({ content: '姓名或单位名称，不能为空' });
            that.$refs['C2'].$refs['reg_people_name'].focus();
            that.$refs['C2'].reg_people_name = '';
            return
        }

        if (!$app.judgeData($app.delSpace(obj.reg_people_contact))) {
            that.$Message.error({ content: '联系人，不能为空' });
            that.$refs['C2'].$refs['reg_people_contact'].focus();
            that.$refs['C2'].reg_people_contact = '';
            return
        }

        if (!$app.judgeData($app.delSpace(obj.reg_people_phone))) {
            that.$Message.error({ content: '手机号码，不能为空' });
            that.$refs['C2'].$refs['reg_people_phone'].focus();
            that.$refs['C2'].reg_people_phone = '';
            return
        }
        if (!$app.telCheck(obj.reg_people_phone)) {
            that.$Message.error({ content: '请输入正确的手机格式，勿含空格等特殊字符' });
            that.$refs['C2'].$refs['reg_people_phone'].focus();
            return
        }



        if (!$app.judgeData($app.delSpace(obj.reg_people_tel))) {
            that.$Message.error({ content: '联系方式电话，不能为空' });
            that.$refs['C2'].$refs['reg_people_tel'].focus();
            that.$refs['C2'].reg_people_tel = '';
            return
        }



        if (!$app.judgeData($app.delSpace(obj.reg_people_email))) {
            that.$Message.error({ content: '联系方式E-mail，不能为空' });
            that.$refs['C2'].$refs['reg_people_email'].focus();
            that.$refs['C2'].reg_people_email = '';
            return
        }
        if (!$app.mailCheck(obj.reg_people_email)) {
            that.$Message.error({ content: '请输入正确的E-mail格式，勿含空格等特殊字符' });
            that.$refs['C2'].$refs['reg_people_email'].focus();
            return
        }


        if (!$app.judgeData($app.delSpace(obj.reg_people_address))) {
            that.$Message.error({ content: '联系方式收件地址，不能为空' });
            that.$refs['C2'].$refs['reg_people_address'].focus();
            that.$refs['C2'].reg_people_address = '';
            return
        }

        callBack();
    },
}; 